package com.tjetc.service.impl;

import com.tjetc.constant.MessageConstant;
import com.tjetc.domain.BClassify;
import com.tjetc.repository.BClassifyRepository;
import com.tjetc.service.BClassifyService;
import com.tjetc.vo.OperationMessage;
import com.tjetc.vo.Result;
import jakarta.annotation.Resource;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

import static com.tjetc.constant.CodeConstant.SUBMIT;
import static com.tjetc.constant.RedisConstant.OPERATION_KEY;
import static com.tjetc.constant.RedisConstant.STATE;

/**
 * @author zc
 * @Date 2025/7/16 下午8:33
 */
@Service
public class BClassifyServiceImpl implements BClassifyService {
    @Autowired
    private BClassifyRepository bClassifyRepository;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public Result findAll() {
        //查出所有的,但是是Iterable,需要转list
        Iterable<BClassify> result = bClassifyRepository.findAll();
        //创建一个新的list,然后手动遍历添加到新集合中
        List<BClassify> list = new ArrayList<>();
        result.forEach(list::add);
        return Result.ok(list);
    }

    @Override
    public Result importAll() {
        try {
            OperationMessage operationMessage = OperationMessage.importAll();
            rabbitTemplate.convertAndSend("topicExchange","topic.bClassify",operationMessage);
            HashOperations<String, Object, Object> ops = stringRedisTemplate.opsForHash();
            ops.put(OPERATION_KEY + operationMessage.getOperationId(),STATE,SUBMIT.toString());
            stringRedisTemplate.expire(OPERATION_KEY + operationMessage.getOperationId(), 1, TimeUnit.SECONDS);
            return Result.submit(operationMessage.getOperationId());
        } catch (AmqpException e) {
            e.printStackTrace();
        }
        return Result.fail();
    }

    @Override
    public Result deleteAll() {
        try {
            //创建一个消息,交给消息队列,接收端操作完对应的操作后根据消息id修改redis中的消息状态
            OperationMessage operationMessage = OperationMessage.deleteAll();
            rabbitTemplate.convertAndSend("topicExchange","topic.bClassify",operationMessage);
            //往redis里放消息的状态,设置过期时间,并且返回加载中状态和消息id给前端,前端发现是该状态后根据传过去的消息id去每0.5秒查询一次状态
            HashOperations<String, Object, Object> ops = stringRedisTemplate.opsForHash();
            ops.put(OPERATION_KEY + operationMessage.getOperationId(),STATE,SUBMIT.toString());
            stringRedisTemplate.expire(OPERATION_KEY + operationMessage.getOperationId(), 1, TimeUnit.SECONDS);
            return Result.submit(operationMessage.getOperationId());
        } catch (AmqpException e) {
            e.printStackTrace();
        }
        return Result.fail();
    }
}
